package com.bajiao.service.store.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.util.FastJsonHelper;
import com.bajiao.common.util.vans.RegexTool;
import com.bajiao.dto.store.StoreApplyRequest;
import com.bajiao.dto.store.StoreApplyStep1Request;
import com.bajiao.entity.store.StoreApply;
import com.bajiao.mapper.store.StoreApplyMapper;
import com.bajiao.service.store.StoreApplyService;
import com.bajiao.vo.store.StoreApplyAdmin;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 店铺申请表
 * @Author: FanJq
 * @Date:   2024-12-24
 * @Version: V1.0
 */
@Service
public class StoreApplyServiceImpl extends ServiceImpl<StoreApplyMapper, StoreApply> implements StoreApplyService {

    /**
     * @Description
     * 提交入驻
     * @date 2024/12/24 16:23
     * @author FanJq
     * @param request
     * @return
     */
    @Override
    public void apply(String userId, StoreApplyRequest request) {
        StoreApply storeApply = this.getByUserId(userId);
        if (ObjectUtil.isNull(storeApply)) {
            //@create by FanJq 2024/12/24 16:36 验证参数
            this.verifyApply(request);
            storeApply = FastJsonHelper.beanToBean(request, StoreApply.class);
            storeApply.setUserId(userId);
            super.save(storeApply);
        } else {
            switch (storeApply.getStatus().intValue()) {
                case _Whole.ZERO: throw new DataHandleException("您的申请已提交，请等待平台审核");
                case _Whole.ONE: throw new DataHandleException("您的申请已通过");
            }
            //@create by FanJq 2024/12/24 16:36 验证参数
            this.verifyApply(request);
            BeanUtils.copyProperties(request, storeApply);
            storeApply.setStatus(_Whole.ZERO);
            super.updateById(storeApply);
        }
    }

    /**
     * @Description
     * 根据用户id获取
     * @date 2024/12/24 16:39
     * @author FanJq
     * @param userId
     * @return {@link StoreApply}
    */
    @Override
    public StoreApply getByUserId(String userId) {
        return super.lambdaQuery().eq(StoreApply::getUserId, userId).one();
    }

    /**
     * @Description
     * 根据商户id获取
     * @date 2024/12/28 11:37
     * @author FanJq
     * @param storeId
     * @return {@link StoreApply}
    */
    @Override
    public StoreApply getByStoreId(String storeId) {
        return super.lambdaQuery().eq(StoreApply::getStoreId, storeId).one();
    }

    /**
     * @Description
     * 管理后台列表
     * @date 2025/1/6 16:34
     * @author FanJq
     * @param
     * @return {@link List< StoreApplyAdmin>}
    */
    @Override
    public List<StoreApplyAdmin> getAdminList(String title, String storeMobile, String legalPerson, String mobile, Integer status) {
        return super.baseMapper.getAdminList(title, storeMobile, legalPerson, mobile, status);
    }


    /**
     * @Description
     * 验证参数
     * @date 2024/12/24 15:27
     * @author FanJq
     * @param request
     * @return
    */
    public void verifyApply(StoreApplyRequest request) {
        if (!RegexTool.idCardCheck(request.getIdCard()))
            throw new DataHandleException("请填写正确的身份证号码");
        if (request.getType().intValue() == _Whole.TWO) {
            if (StrUtil.isBlank(request.getCompanyName()))
                throw new DataHandleException("请填写商户名称");
            if (request.getCompanyName().length() > 20)
                throw new DataHandleException("商户名称过长");
            if (StrUtil.isBlank(request.getCompanyIntro()))
                throw new DataHandleException("请填写公司简介");
            if (request.getCompanyName().length() > 200)
                throw new DataHandleException("公司简介过长");
            if (StrUtil.isBlank(request.getLicenseImage()))
                throw new DataHandleException("请上传营业执照");
            if (StrUtil.isBlank(request.getLicenseNumber()))
                throw new DataHandleException("请填写营业执照编号");
            if (ObjectUtil.isNull(request.getLicenseType()))
                throw new DataHandleException("请选择营业执照类型");
            if (request.getLicenseType() < 1 || request.getLicenseType() > 2)
                throw new DataHandleException("请选择有效的营业执照类型");
            if (ObjectUtil.isNull(request.getLicenseStartDate()))
                throw new DataHandleException("请选择营业执照生效时间");
            if (ObjectUtil.isNull(request.getLicenseEndDate()))
                throw new DataHandleException("请选择营业执照失效时间");
            if (request.getLicenseStartDate().getTime() >= request.getLicenseEndDate().getTime())
                throw new DataHandleException("请选择正确的营业执照有效期");
            if (ObjectUtil.isNull(request.getFoundDate()))
                throw new DataHandleException("请选择成立时间");
        }
    }
}
